//build 是将所有context menu的事件初始化，以及哪些可用，哪些不可用
function buildMenu(location,canvas) {
	for ( var i in _dss_editor.canvasContextItems) {
		var item = _dss_editor.canvasContextItems[i];
		item.onclick = function(e) {
			var eventObj = {};
			eventObj.eventType = this.event;
			eventObj.location = location;
			eventObj.canvas = canvas;
			_dss_editor.fireCommandOp(eventObj);
		};		
	}

}

// easy UI 需要先将这些节点创建出来------------------------------------------

//创建canvas的 右键菜单

function createContextMenu(location,canvasViewer) {
	$('#mycontext').menu("destroy");
	var parentNode = $F('canvas');
	var menu = $C('div');
	var selected = canvasViewer.selectionDiagrams;
	menu.id = 'mycontext';
	menu.style.width = '120px';
	parentNode.appendChild(menu);	
	$('#mycontext').menu({
		
	}); 
	createEditMenu(location,canvasViewer,selected);
	createShowMenu(location,canvasViewer);
	createOtherMenu(location,canvasViewer);
	
	$(function() {
		$(canvas).bind('contextmenu', function(e) {
			e.preventDefault();
			$('#mycontext').menu('show', {
				left : e.pageX,
				top : e.pageY
			});
		});
	});
}

function createEditMenu(location,canvasViewer,selected) {
	$('#mycontext').menu('appendItem', {
		id:'Edit',
		text: 'Edit',
		iconCls: 'icon-edit'
	});	
	var item = $('#mycontext').menu('findItem', 'Edit'); 	
	
	//if(selected.length > 0){
		createConextMenuItem(location,canvasViewer,item,'Copy','',CMD_COPY);
		createConextMenuItem(location,canvasViewer,item,'Cut','icon-cut',CMD_CUT);
		createConextMenuItem(location,canvasViewer,item,'Paste','',CMD_PASTE);
		createConextMenuItem(location,canvasViewer,item,'Delete','',CMD_DELETE);
		createConextMenuItem(location,canvasViewer,item,'Record','',CMD_RECORD);
		createConextMenuItem(location,canvasViewer,item,'Change Type','',CMD_CHANGE_TYPE);
		createConextMenuItem(location,canvasViewer,item,'Instantiate','',CMD_INSTANTIATE);
		createConextMenuItem(location,canvasViewer,item,'No Instantiate','',CMD_NO_INSTANTIATE);
		createConextMenuItem(location,canvasViewer,item,'Add Shadow','',CMD_ADD_SHADOW);
		createConextMenuItem(location,canvasViewer,item,'Remove Shadow','',CMD_REMOVE_SHADOW);
		createConextMenuItem(location,canvasViewer,item,'Drill Down','',CMD_DRILL_DOWN);
		createConextMenuItem(location,canvasViewer,item,'Drill Up','',CMD_DRILL_UP);
		createConextMenuItem(location,canvasViewer,item,'Edit static count','',CMD_EDIT_STATIC);
		createConextMenuItem(location,canvasViewer,item,'Define user system','',CMD_DEFINE_USER);
		//var last = selected[selected.length-1];
		//if(last.style == SYSTEM && last.hasShadow()){
			createConextMenuItem(location,canvasViewer,item,'Edit Array','',CMD_EDIT_ARRAY);
	//	}
	//}
	
	
}



function createShowMenu(location,canvasViewer) {
	$('#mycontext').menu('appendItem', {
		text: 'Show',
		iconCls: ''
	});
	var item = $('#mycontext').menu('findItem', 'Show'); 
	createConextMenuItem(location,canvasViewer,item,'Layout','',CMD_LAYOUT);
	createConextMenuItem(location,canvasViewer,item,'Complexity','',CMD_COMPLEXITY);
	createConextMenuItem(location,canvasViewer,item,'Change mark','',CMD_CHANGE_MARK);
	createConextMenuItem(location,canvasViewer,item,'Syntax error','',CMD_SYNTAX_ERROR);
	createConextMenuItem(location,canvasViewer,item,'Show content','',CMD_SHOW_CONTENT);
	createConextMenuItem(location,canvasViewer,item,'Hide content','',CMD_HIDE_CONTENT);
	createConextMenuItem(location,canvasViewer,item,'Show name','',CMD_SHOW_NAME);
	createConextMenuItem(location,canvasViewer,item,'Hide name','',CMD_HIDE_NAME);
}



function createOtherMenu(location,canvasViewer) {
	$('#mycontext').menu('appendItem', {
		text: 'Other',
		iconCls: ''
	});
	var item = $('#mycontext').menu('findItem', 'Other'); 
	createConextMenuItem(location,canvasViewer,item,'Save','icon-save',CMD_SAVE);
	createConextMenuItem(location,canvasViewer,item,'Save as','',CMD_SAVE_AS);
	createConextMenuItem(location,canvasViewer,item,'Export slides','',CMD_EXPORT_SLIDE);	
	createConextMenuItem(location,canvasViewer,item,'Import','',CMD_IMPORT);
	createConextMenuItem(location,canvasViewer,item,'Derive','',CMD_DERIVE);
	createConextMenuItem(location,canvasViewer,item,'Generate document','',CMD_GENERATE_DOC);
	createConextMenuItem(location,canvasViewer,item,'Generate code','',CMD_GENERATE_CODE);
	createConextMenuItem(location,canvasViewer,item,'Search','',CMD_SEARCH);
}

function createShowMenuItems(id){
	var items = $C('div');
	items.setAttribute("id", id);
	items.appendChild(createMenuItem('Layout', '', CMD_LAYOUT,id + 'layout'));
	items.appendChild(createMenuItem('Complexity', '', CMD_COMPLEXITY,id + 'complexity'));
	items.appendChild(createMenuItem('Change mark', '', CMD_CHANGE_MARK,id + 'changemark'));
	items.appendChild(createMenuItem('Syntax error', '', CMD_SYNTAX_ERROR,id + 'error'));
	items.appendChild(createMenuItem('Show content', '', CMD_SHOW_CONTENT,id + 'showcontent'));
	items.appendChild(createMenuItem('Hide content', '', CMD_HIDE_CONTENT,id + 'hidecontent'));
	items.appendChild(createMenuItem('Show name', '', CMD_SHOW_NAME,id + 'showname'));
	items.appendChild(createMenuItem('Hide name', '', CMD_HIDE_NAME,id + 'hidename'));
	return items;
}
function createOtherMenuItems(id){
	var items = $C('div');
	items.setAttribute("id", id);
	items.appendChild(createMenuItem('Save', 'icon-save', CMD_SAVE,id + 'save'));
	items.appendChild(createMenuItem('Save as', '', CMD_SAVE_AS,id + 'saveas'));
	items.appendChild(createMenuItem('Derive', '', CMD_DERIVE,id + 'derive'));
	items.appendChild(createMenuItem('Generate slides', '',
					CMD_GENERATE_SLIDES,id + 'gslides'));
	items.appendChild(createMenuItem('Generate document', '',
					CMD_GENERATE_DOC,id + 'gdocument'));
	items.appendChild(createMenuItem('Generate code', '', CMD_GENERATE_CODE,id + 'gcode'));
	items.appendChild(createMenuItem('Search', 'icon-search', CMD_SEARCH,id + 'search'));
	return items;
}
function createEditMenuItems(id){
	var items = $C('div');
	items.setAttribute("id", id);
	items.appendChild(createMenuItem('Copy', '', CMD_COPY,id + 'copy'));
	items.appendChild(createMenuItem('Cut', 'icon-cut', CMD_CUT,id + 'cute'));
	items.appendChild(createMenuItem('Paste', '', CMD_PASTE,id + 'past'));
	items.appendChild(createMenuItem('Delete', '', CMD_DELETE,id + 'delete'));
	items.appendChild(createMenuItem('Record', '', CMD_RECORD,id + 'record'));
	items.appendChild(createMenuItem('Change Type', '', CMD_CHANGE_TYPE,id + 'changetype'));
	items.appendChild(createMenuItem('Instantiate', '', CMD_INSTANTIATE,id + 'instantiate'));
	items.appendChild(createMenuItem('No Instantiate', '', CMD_NO_INSTANTIATE,id + 'noinstantate'));
	items.appendChild(createMenuItem('Add Shadow', '', CMD_ADD_SHADOW,id + 'addshadow'));
	items.appendChild(createMenuItem('Remove Shadow', '', CMD_REMOVE_SHADOW,id + 'removeshadow'));
	items.appendChild(createMenuItem('Drill Down', '', CMD_DRILL_DOWN,id + 'drilldown'));
	items.appendChild(createMenuItem('Drill Up', '', CMD_DRILL_UP,id + 'drillup'));
	items.appendChild(createMenuItem('Edit static count', '', CMD_EDIT_STATIC,id + 'staticcnt'));
	items.appendChild(createMenuItem('Define user system', '',CMD_DEFINE_USER,id + 'usersystem'));
	return items;
}

function createConextMenuItem(location,canvas,item,text,icon,event){
	$('#mycontext').menu('appendItem', {
		parent: item.target,  // the parent item element
		text: text,
		iconCls: icon,
		onclick: function(){
			var eventObj = createEventObj(location,canvas,event);
			_dss_editor.fireCommandOp(eventObj);			
		}
	});
}


function createMenuItem(label, icon, event,id) {
	var menuItem = $C('div');
	menuItem.innerHTML = label;
	menuItem.icon = icon;
	menuItem.event = event;
	this.id = id;
	if (icon != '') {
		menuItem.setAttribute("data-options", "iconCls:'" + icon + "'");
	}
	_dss_editor.canvasContextItems[id] = menuItem;
	return menuItem;
}

//cmd line
function createCmdLine(){
	var cmdLine = $F('cmdLine');
	cmdLine.appendChild(createEditMenuItems('edit'));	
	cmdLine.appendChild(createOtherMenuItems('other'));
	cmdLine.appendChild(createShowMenuItems('show'));
	createCmdCanvas(cmdLine);	
}

function createCmdCanvas(parentNode){
	// <canvas class="myCanvas" id = canvasLayer'+ id+'  width="800px" height="100px" contextmenu="mycontext" background=#eaf2ff >	your browser does not support the canvas tag </canvas>
	var cmd = $C('canvas');
	var cmdDiv = $C('div');
	cmd.id = CANVAS_COMMAND;
	cmd.width = 800;
	cmd.height = 60;
	cmdDiv.appendChild(cmd);
	cmdLine.appendChild(cmdDiv);
	
	var canvas = new Canvas();
	canvas.entity = cmd;
	canvasViewerEventInit(canvas);
	_dss_editor.canvas[CANVAS_COMMAND] = canvas;
	canvas.type = CANVAS_COMMAND;
	canvas.sysForView.complete = {};
	canvas.sysForView.complete.sys = [];
	canvas.sysForView.complete.links = [];
//	canvas.sys = canvas.sysForView.complete.sys;
	canvas.links = canvas.sysForView.complete.links;
	_dss_editor._model.system.initSys(canvas);
	canvas.layout = function(){
		var x = 10;
		for(var i = 0 ;i< this.sys.length;i++){
			var system = this.sys[i];
			var rectangle = {x:x,y:5,w:system.getRectangle().w,h:labelHeight};
			system.setShowContent(false);
			system.setRectangle(rectangle);
			x += system.getRectangle().w + 10;
		}
	};
	
}

function createEventObj(location,canvas,event){
	var eventObj = {};
	eventObj.eventType = event;
	eventObj.location = location;
	eventObj.canvas = canvas;
	return eventObj;
}
// palette conext menu
function createPaletteConextMenu() {

}